package jp.ac.u_tokyo.dch.dao.psi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.model.xml.psi.Psi;
import jp.ac.u_tokyo.dch.model.xml.psi.PsiObject;
import jp.ac.u_tokyo.dch.util.HttpConnectUtil;
import jp.ac.u_tokyo.dch.util.XmlCommonUtil;
import jp.ac.u_tokyo.dch.util.common.MessageResourceUtil;
import jp.ac.u_tokyo.dch.view.Constants;

public class PsiInterfaceDao {


	private String PSI_SERVER_URL = "";

	public PsiInterfaceDao() {
		PSI_SERVER_URL = MessageResourceUtil.getText("PSI_SERVER_URL");
	}

	/**
	 * データ検索
	 * @param searchKeyMap 検索項目
	 * @return 検索件数
	 * @throws DBException 例外
	 */
	public int getSearchResultCnt(Map<String, String> searchKeyMap) throws Exception {

		Map<String, String> params = new HashMap<String, String>();
		params.put("name_jp", searchKeyMap.get("name_jp"));
		params.put("name_kana", searchKeyMap.get("name_kana"));
		params.put("name_eng", searchKeyMap.get("name_eng"));
		params.put("alias_jp", searchKeyMap.get("alias_jp"));
		params.put("alias_kana", searchKeyMap.get("alias_kana"));
		params.put("alias_eng", searchKeyMap.get("alias_eng"));
		params.put("latitude", searchKeyMap.get("latitude"));
		params.put("longitude", searchKeyMap.get("longitude"));
		params.put("birth_from", searchKeyMap.get("birth_from"));
		params.put("birth_to", searchKeyMap.get("birth_to"));
		params.put("death_from", searchKeyMap.get("death_from"));
		params.put("death_to", searchKeyMap.get("death_to"));
		params.put("person_url", searchKeyMap.get("person_url"));
		params.put("relatede_url", searchKeyMap.get("relatede_url"));
		params.put("psi", searchKeyMap.get("psi"));
		params.put("desc", searchKeyMap.get("desc"));

		String result = HttpConnectUtil.httpRequest(PSI_SERVER_URL + "/searchNum", params);

		Psi psi = XmlCommonUtil.xmlToObject(result, Psi.class);

		if (Constants.PSI_ERR.equals(psi.getResponse().getStatus())) {
			return -1;
		}

		String num = psi.getResponse().getResponseData().getNum();

		return Integer.valueOf(num);
	}


	/**
	 * 検索して結果を返す
	 * @param start 開始インデックス
	 * @param end 終了インデックス
	 * @param searchKeyMap 検索項目
	 * @return 検索結果
	 * @throws Exception 例外
	 */
	public List<PsiObject> getSearchResult(int start, int viewPageCnt, Map<String, String> searchKeyMap) throws Exception {

		Map<String, String> params = new HashMap<String, String>();

		params.put("name_jp", searchKeyMap.get("name_jp"));
		params.put("name_kana", searchKeyMap.get("name_kana"));
		params.put("name_eng", searchKeyMap.get("name_eng"));
		params.put("alias_jp", searchKeyMap.get("alias_jp"));
		params.put("alias_kana", searchKeyMap.get("alias_kana"));
		params.put("alias_eng", searchKeyMap.get("alias_eng"));
		params.put("latitude", searchKeyMap.get("latitude"));
		params.put("longitude", searchKeyMap.get("longitude"));
		params.put("birth_from", searchKeyMap.get("birth_from"));
		params.put("birth_to", searchKeyMap.get("birth_to"));
		params.put("death_from", searchKeyMap.get("death_from"));
		params.put("death_to", searchKeyMap.get("death_to"));
		params.put("person_url", searchKeyMap.get("person_url"));
		params.put("relatede_url", searchKeyMap.get("relatede_url"));
		params.put("psi", searchKeyMap.get("psi"));
		params.put("desc", searchKeyMap.get("desc"));
		params.put("searchnum", String.valueOf(viewPageCnt));
		params.put("searchfrom", String.valueOf(start));
		params.put("sortkey", "subject_identifier");


		String result = HttpConnectUtil.httpRequest(PSI_SERVER_URL + "/search", params);
		Psi psi = XmlCommonUtil.xmlToObject(result, Psi.class);

		return psi.getResponse().getResponseData().getDataset();
	}

	/**
	 * 検索して結果を返す
	 * @param start 開始インデックス
	 * @param end 終了インデックス
	 * @param searchKeyMap 検索項目
	 * @return 検索結果
	 * @throws Exception 例外
	 */
	public Psi selectById(String psiid) throws Exception {

		Map<String, String> params = new HashMap<String, String>();

		params.put("psi", psiid);

		String result = HttpConnectUtil.httpRequest(PSI_SERVER_URL + "/select", params);

		Psi psi = XmlCommonUtil.xmlToObject(result, Psi.class);

		return psi;
	}


	public Psi insert(PsiObject psiObject) {

		Map<String, String> params = new HashMap<String, String>();

		params.put("psi", psiObject.getPsi());
		params.put("name_jp", psiObject.getName_jp());
		params.put("name_kana", psiObject.getName_kana());
		params.put("name_eng", psiObject.getName_eng());
		params.put("alias_jp", psiObject.getAlias_jp());
		params.put("alias_kana", psiObject.getAlias_kana());
		params.put("desc", psiObject.getDescription());
		params.put("latitude", psiObject.getLatitude());
		params.put("longitude", psiObject.getLongitude());
		params.put("alias_eng", psiObject.getAlias_eng());
		params.put("birth", psiObject.getBirth());
		params.put("death", psiObject.getDeath());
		params.put("relate_url", psiObject.getRelated_url());
		params.put("person_url", psiObject.getPerson_url());
		params.put("published", psiObject.getPublished_by());
		params.put("lastmodified", psiObject.getLast_modified_by());

		String result = HttpConnectUtil.httpRequest(PSI_SERVER_URL + "/add", params);

		return XmlCommonUtil.xmlToObject(result, Psi.class);


	}

	public Psi delete(PsiObject psiObject) {

		Map<String, String> params = new HashMap<String, String>();

		params.put("psi", psiObject.getPsi());
		params.put("ver", psiObject.getVer());
		params.put("lastModified", psiObject.getLast_modified_by()	);

		String result = HttpConnectUtil.httpRequest(PSI_SERVER_URL + "/del", params);

		return XmlCommonUtil.xmlToObject(result, Psi.class);
	}

	public Psi update(PsiObject psiObject) {

		Map<String, String> params = new HashMap<String, String>();

		params.put("psi", psiObject.getPsi());
		params.put("name_jp", psiObject.getName_jp());
		params.put("name_kana", psiObject.getName_kana());
		params.put("name_eng", psiObject.getName_eng());
		params.put("alias_jp", psiObject.getAlias_jp());
		params.put("alias_kana", psiObject.getAlias_kana());
		params.put("desc", psiObject.getDescription());
		params.put("latitude", psiObject.getLatitude());
		params.put("longitude", psiObject.getLongitude());
		params.put("alias_eng", psiObject.getAlias_eng());
		params.put("birth", psiObject.getBirth());
		params.put("death", psiObject.getDeath());
		params.put("relate_url", psiObject.getRelated_url());
		params.put("person_url", psiObject.getPerson_url());
		params.put("published", psiObject.getPublished_by());
		params.put("lastmodified", psiObject.getLast_modified_by());
		params.put("ver", psiObject.getVer());

		String result = HttpConnectUtil.httpRequest(PSI_SERVER_URL + "/edit", params);
		return XmlCommonUtil.xmlToObject(result, Psi.class);

	}
}
